import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test4_callable_FutureTask {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //方式一：内部类
        FutureTask<Integer>task1=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                //任务是累加和
                int count=0;
                for (int i=0;i<10;i++){
                    Thread.sleep(1000);
                    count+=i;
                }
                return count;
            }
        });
        //将任务类与线程绑定
        Thread t1=new Thread(task1);
        t1.start();
        //方式二：lambda表达式写法
        FutureTask<Integer>task2=new FutureTask<>(()->{
            int count=0;
            for (int i=0;i<10;i++){
                count+=i;
            }return count;
        });
        //将任务类与线程绑定
        Thread t2=new Thread(task2);
        t2.start();
        //有返回值
        System.out.println("累加的和为："+task1.get());
        System.out.println("累加的和为："+task2.get());
        System.out.println("请理解什么是阻塞式...");
    }
}
